package com.ruicar.afs.cloud.account.common.controller;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruicar.afs.cloud.account.common.condition.VoucherFlowInfoCondition;
import com.ruicar.afs.cloud.account.common.entity.VoucherFlowInfo;
import com.ruicar.afs.cloud.account.common.service.VoucherFlowInfoService;
import com.ruicar.afs.cloud.batch.entity.AfsBatchInfo;
import com.ruicar.afs.cloud.batch.service.AfsBatchInfoService;
import com.ruicar.afs.cloud.common.core.query.QueryCondition;
import com.ruicar.afs.cloud.common.core.util.IResponse;
import com.ruicar.afs.cloud.common.modules.dto.mq.voucher.VoucherFlowInfoDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * @author infinity
 * @version 1.0
 * @date 2020/6/18 10:29
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/voucherFlow")
@Api(value = "voucherFlow", description = "凭证记录信息")
public class VoucherFlowController {
    private final VoucherFlowInfoService voucherFlowInfoService;
    private final AfsBatchInfoService afsBatchInfoService;
    @PostMapping("/save")
    @ApiOperation(value = "保存凭证记录信息")
    public IResponse<Boolean> save(@RequestBody @Validated VoucherFlowInfoDto voucherFlowInfoDto) {
        AfsBatchInfo afsBatchInfo = afsBatchInfoService.getSystemBatchInfo("icos"); // 判断是否在宽限期内
        Date date = DateUtil.parse(afsBatchInfo.getBatchDate(), DatePattern.PURE_DATE_PATTERN);
        voucherFlowInfoDto.setKeepAccountDate(date);
        return IResponse.success(voucherFlowInfoService.checkAndSaveFlow(voucherFlowInfoDto));
    }

    @PostMapping("/getVoucherFlowList")
    @ApiOperation(value = "获取凭证记录信息")
    public IResponse<Object> getVoucherFlowList(@RequestBody QueryCondition<VoucherFlowInfoCondition> condition) {
        IPage iPage = voucherFlowInfoService.page(new Page<>(condition.getPageNumber(), condition.getPageSize()),
                Wrappers.<VoucherFlowInfo>lambdaQuery()
                        .like(StringUtils.isNotBlank(condition.getCondition().getBuriedPointNo()), VoucherFlowInfo::getBuriedPointNo, StringUtils.isNotBlank(condition.getCondition().getBuriedPointNo()) ? condition.getCondition().getBuriedPointNo().trim() : "")
                        .eq(StringUtils.isNotEmpty(condition.getCondition().getStatus()), VoucherFlowInfo::getStatus, condition.getCondition().getStatus())
                        .like(StringUtils.isNotBlank(condition.getCondition().getContractNo()), VoucherFlowInfo::getContractNo, StringUtils.isNotBlank(condition.getCondition().getContractNo()) ? condition.getCondition().getContractNo().trim() : ""));
        return IResponse.success(iPage);
    }

    @PostMapping("resetFlowStatus/{id}")
    @ApiOperation(value = "重置凭证流水状态")
    public IResponse resetFlowStatus(@PathVariable String id) {
        return IResponse.success(voucherFlowInfoService.resetFlowStatus(Long.parseLong(id)));
    }

    @PostMapping("resetItemStatus/{id}")
    @ApiOperation(value = "重置辅助帐发送状态")
    public IResponse resetItemStatus(@PathVariable String id) {
        return IResponse.success(voucherFlowInfoService.resetItemStatus(Long.parseLong(id)));
    }
}
